<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="phone"><el-input v-model="ruleForm.phone"></el-input></el-form-item>
            <el-form-item label="密码" prop="password"><el-input type="password" v-model="ruleForm.password" auto-complete="off"></el-input></el-form-item>
            <el-form-item label="确认密码" prop="checkPass"><el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input></el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Register",
        inject:['reload'],//注入reload方法
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };

            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };

            return {
                activeName: 'second',
                ruleForm: {
                    phone: '',
                    password: '',
                    checkPass: ''
                },
                rules: {
                    phone: [{ required: true, message: '请输入您的名称', trigger: 'blur' }, { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }],
                    password: [{ required: true, validator: validatePass, trigger: 'blur' }],
                    checkPass: [{ required: true, validator: validatePass2, trigger: 'blur' }]
                }
            };
        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {

                        this.$http.post("account/account/registered", this.ruleForm).then(data => {
                            if (data) {
                                this.$message({
                                    type: 'success',
                                    message: '注册成功!'
                                });
                                // this.$router.push("/")
                                this.reload();
                            } else {
                                this.$message({
                                    type: 'info',
                                    message: '注册失败,用户名已存在!'
                                });
                                // this.$router.push("/main/login")
                                this.ruleForm = ''
                                this.reload();
                            }
                        })
                    }else {
                        console.log('error submit!!')
                        return false;
                    }
                })
            },

            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    };
</script>
